package com.cheli.managerAsset.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheli.commonutils.JwtUtils;
import com.cheli.commonutils.R;
import com.cheli.managerAsset.client.ManagerClient;
import com.cheli.managerAsset.entity.AsAsset;
import com.cheli.managerAsset.entity.AssetStaff;
import com.cheli.managerAsset.entity.frontvo.AssetFrontVo;
import com.cheli.managerAsset.entity.vo.AssetInfoVo;
import com.cheli.managerAsset.entity.frontvo.AssetStaffVo;
import com.cheli.managerAsset.entity.vo.AssetStaffBackVo;
import com.cheli.managerAsset.service.AsAssetService;
import com.cheli.managerAsset.service.AssetStaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Classname IndexFrontController
 * @Description 资产界面展示后端
 * @Date 2021/5/3 18:03
 * @Created by cl
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/asManager/assetFront")
public class IndexFrontController {

    @Autowired
    private AsAssetService asAssetService;

    @Autowired
    private AssetStaffService assetStaffService;

    @Autowired
    public ManagerClient managerClient;

   @PostMapping("getFrontAssetList/{current}/{limit}")
    public R getFrontAssetList(@PathVariable Long current,
                               @PathVariable Long limit,
                               @RequestBody(required = false) AssetFrontVo assetFrontVo){
        Page<AsAsset> page = new Page<>(current, limit);
        Map<String, Object> map = asAssetService.getFrontAssetList(page, assetFrontVo);
        return R.ok().data(map);
    }

   @PostMapping("getBackAssetList/{current}/{limit}")
    public R getBackAssetList(@PathVariable Long current,
                               @PathVariable Long limit,
                               @RequestBody(required = false) AssetStaff assetStaff,
                              HttpServletRequest request){
        log.info("借出归还分页条件查询"+assetStaff);
       String id = JwtUtils.getMemberIdByJwtToken(request);
       List<AssetStaffBackVo> records = assetStaffService.getBackAssetList(id);
        int total = records.size();
        return R.ok().data("total", total).data("items", records);
    }

   @GetMapping("getFrontAssetInfo/{assetId}")
    public R getFrontAssetInfo(@PathVariable("assetId") String assetId){
       AssetInfoVo info = asAssetService.getAssetInfo(assetId);
        return R.ok().data("info",info);
    }

    /**
     * 借出资产
     * @param assetStaffVo
     * @return
     */
    @PostMapping("outAsset")
    public R outAsset(@RequestBody AssetStaffVo assetStaffVo, HttpServletRequest request){
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        assetStaffVo.setStaffId(uid);
        assetStaffService.outAsset(assetStaffVo);
        return R.ok();
    }

    /**
     * 归还资产
     * @param id
     * @return
     */
    @GetMapping("backAsset/{id}")
    public R backAsset(@PathVariable String id, HttpServletRequest request){
        log.info("归还资产"+id);
       String uid = JwtUtils.getMemberIdByJwtToken(request);
        assetStaffService.backAsset(uid,id);
        return R.ok();
    }

    /**
     * 申请资产
     * @param id
     * @return
     */
    @GetMapping("repairAsset/{id}")
    public R repairAsset(@PathVariable String id, HttpServletRequest request){
        log.info("申请资产"+id);
        AsAsset asset = asAssetService.getById(id);
        asset.setStatus("Repair");
        asAssetService.updateById(asset);
        return R.ok();
    }
}
